package org.snail.employee.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.snail.common.ExcelUtils;
import org.snail.employee.bean.*;
import org.snail.employee.service.impl.EmployeesServiceImpl;
import org.snail.employee.bean.EmployeesDO;
import org.snail.exception.*;
import org.snail.exception.BaseException;
import org.snail.exception.EmpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ghb on 2017/12/12.
 */
@Controller
@RequestMapping("employees")
public class EmployeesController {

    private static final Logger LOGGER =  Logger.getLogger(EmployeesController.class);

    @Autowired
    private EmployeesServiceImpl employeesServiceImpl;

    /**
     *  保存员工信息v2
     * @param
     * @return
     * @RequestBody EmployeesSaveEmpList employeesSaveEmpList
     */
    @RequestMapping("saveEmpList_v2")
    @ResponseBody
    public  void saveEmp_v2(@RequestBody EmployeesSaveEmpList employeesSaveEmpList) throws InvocationTargetException, IllegalAccessException, EmpException {
       /*
       *   测试保存通讯录表
       * */
        employeesServiceImpl.saveEmpList_v2(employeesSaveEmpList);
    }

    /**
     *  保存员工信息v2
     * @param
     * @return
     * @RequestBody @RequestBody List<EmployeesDO> list, @RequestParam("empType") String empType
     */
    @RequestMapping("updateEmp")
    @ResponseBody
    public  JsonResult updateEmp(EmployeesDO employeesDO) throws BaseException {
        JsonResult jsonResult = new JsonResult();
        employeesServiceImpl.updateEmp(employeesDO);
        return jsonResult;
    }


    /**
     * 前端预览的时候通过state 返回与数据库对比后的数据
     * @param file
     * @param empType
     * @param request
     * @return
     * @throws BaseException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RequestMapping("previewEmpList_v2")
    @ResponseBody
    public JsonResult previewEmpList_v2(@RequestParam("file") CommonsMultipartFile file,@RequestParam("empType") String empType,HttpServletRequest request) throws BaseException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
        JsonResult result = new JsonResult();
        List list = employeesServiceImpl.preview_v2(file,empType,request);
        result.setData(list);
        return result;
    }
    /**
     *  根据以下条件查询员工
     *      //员工姓名
     *      private String name;
     *      //佰钧成工号
     *      private String workNum;
     *      //起始页
     *      private Integer startPage;
     *      //每页显示的数量
     *      private Integer pageSize;
     *
     * @param empCondition
     * @return
     */
    @RequestMapping("findAllEmp")
    @ResponseBody
    public Map<String,PageInfo> findAllEmp(EmpCondition empCondition){
        Map<String,PageInfo> map = employeesServiceImpl.findAllEmp(empCondition);
        return map;
    }


    /**
     * 通过id查询员工
     * @param empCondition
     * @return
     */
    @RequestMapping("findEmpById")
    @ResponseBody
    public JsonResult findEmpById(EmpCondition empCondition) throws EmpException {
        JsonResult result = new JsonResult();
        EmployeesDO employeesDO =  employeesServiceImpl.findEmpById(empCondition);
        result.setData(employeesDO);
        return result;
    }

    @RequestMapping("deleteEmp")
    @ResponseBody
    public JsonResult deleteEmp(@RequestBody List<Integer> idList) throws CommonException {   //需要通过id删除
        JsonResult result = new JsonResult();
        employeesServiceImpl.deleteEmpList(idList);
        return result;
    }

    /**
     * 通过部门ID查询员工   wsss
     * @param empCondition
     * @return
     */
    @RequestMapping("findEmpByEmpCondition")
    @ResponseBody
    public JsonResult findEmpByEmpCondition(@RequestBody EmpCondition empCondition){
        JsonResult result= new JsonResult();
        Map<String, Object> empByEmpCondition = employeesServiceImpl.findEmpByEmpCondition(empCondition);
        result.setData(empByEmpCondition);
        return result;
    }

}
